<template>
  <div>
    <el-dialog
        :visible.sync="loginVisible"
        width="25%"
        class="login_dialog"
        center
        @close="close"
        @open="getCode"
        :close-on-click-modal="false"
    >
      <div class="login">
        <h2 class="login-title">
          {{title}}
        </h2>
        <el-form
            ref="form"
            :rules="rule"
            :model="loginFrom"
            label-width="80px"
            :hide-required-asterisk="true"
        >
          <el-form-item label="账号" prop="username">
            <el-input v-model="loginFrom.username" clearable/>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="loginFrom.password" show-password clearable/>
          </el-form-item>
          <el-form-item label="验证码" prop="code">
            <el-input
                v-model="loginFrom.code"
                auto-complete="off"
                placeholder="验证码"
                style="width: 63%"
                @keyup.enter.native="handleLogin"
            >
<!--              <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />-->
            </el-input>
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" class="login-code-img"/>
            </div>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
    <el-button @click="loginVisible = false">取 消</el-button>
    <el-button type="primary" @click="handleLogin">登 录</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import {getCodeImg} from "@/api/user";
// import svgIcon from "@/components/SvgIcon";
export default {
  name: "index",
  // components:{},
  watch:{
    $route: {
      handler: function(route) {
        this.redirect = route.path
      },
      immediate: true
    },
    uuid:{
      handler:function (newV) {
        this.loginFrom.uuid = newV
      },
      immediate:true
    }
  },
  data() {
    return {
      codeUrl: "",
      uuid:'',
      loginVisible:false,
      loginFrom:{},
      title:'账号登录',
      rule:{
        username:[
          {required:true,message:'账号不能为空',trigger:'blur'}
        ],
        password:[
          {required:true,message:'密码不能为空',trigger:'blur'}
        ],
        code: [{ required: true, trigger: "blur", message: "请输入验证码" }]
      },
      redirect: undefined
    }
  },
  created() {
    // this.getCode();
  },
  methods:{
    reset() {
      this.loginFrom = {
        username:undefined,
        password:undefined,
        code:'',
        uuid: ""
      }
    },
    close() {
      this.reset()
      this.$refs.form.resetFields()
    },
    handleLogin() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$store.dispatch('login',this.loginFrom).then(() => {
            this.$store.dispatch('getInfo')
            this.$router.push({path:this.redirect || '/'})
            this.$modal.msgSuccess('登录成功')
            this.loginVisible = false
          }).catch((err) => {
            console.log(err)
          })
        }
      })
    },
    getCode() {
      getCodeImg().then(res => {
          this.codeUrl = "data:image/gif;base64," + res.data.img;
          this.uuid = res.data.uuid;
      })
    },
  }
}
</script>

<style>
.login-title {
  text-align: center;
}

.login_dialog  .el-dialog__header{
  padding: 0 !important;
}
.login_dialog .el-dialog {
  border-radius: 15px;
}
.login_dialog .el-dialog__body {
  padding-bottom: 0 !important;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
}
.login-code img{
  cursor: pointer;
  vertical-align: middle;
}
.login-code-img {
  height: 38px;
}
.login_dialog .el-form-item__content {
  line-height: 0 !important;
}
</style>
